<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Campaign_model extends Model
{
	private $_host;
	private $_dbname;
	private $_username;
	private $_password;
	private $_port;
	private $_folderImageStore = 'uploads/images/';
	
	private $_host2;
	private $_dbname2;
	private $_username2;
	private $_password2;
	private $_port2;
	
	function Campaign_model()
	{
		parent::Model();
		
        $host = $this->db->hostname;
		$arrHost = explode(':',$host);
		
		$this->_host = trim($arrHost[0]);
		$this->_dbname = $this->db->database;
		$this->_username = $this->db->username;
		$this->_password = $this->db->password;
		$this->_port = $this->db->port;
	}
	
	function getAllUserByGroup($groupId, $strUserId)
	{
		$data =array();
		//$iconn = mysqli_connect($this->_host,$this->_username,$this->_password, $this->_dbname,$this->_port);
//        $result = mysqli_query($iconn, "CALL selfserving_admin_get_all_user_by_group('$groupId', '$strUserId');") or die(mysqli_error());
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_admin_get_all_user_by_group('$groupId', '$strUserId');") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
        {
        	$data[] = $row;
        }
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
        return $data;
	}
	
	function getAllCampaign($userid, $states, $fromdate, $todate, $permitID, $price, $strUserId, $cPage, $numShowItem,$groupid,$strContract)
	{
        $data = array();
        $data['lstCamp'] = array();
        $data['numCurrItem'] = 0;
		$data['iCount'] = 0;
		$data['pCount'] = 0;
        $start = ($cPage - 1) * $numShowItem;
		//$iconn = mysqli_connect($this->_host,$this->_username,$this->_password, $this->_dbname,$this->_port);
  		$iconn = $this->db->conn_id;
  		//var_dump($userid.'/'.$states.'/'.$fromdate.'/'.$todate.'/'.$permitID.'/'.$start.'/'.$numShowItem.'/'.$strUserId.'/'.$groupid.'/'.$strContract);die();
        mysqli_multi_query($iconn, "CALL selfserving_admin_get_al_campaign('$userid', '$states', '$fromdate', '$todate', '$permitID', '$start', '$numShowItem', '$strUserId','$groupid','$strContract');") or die(mysqli_error($iconn));	
         
        $result = mysqli_store_result($iconn);
        $data['numCurrItem'] = mysqli_num_rows($result);
        
        while($row = mysqli_fetch_array($result))
		{
			$row['activate']=date('d/m/Y',strtotime($row['activate']));
            $row['expire']=date('d/m/Y',strtotime($row['expire']));
            if($row['ExpireValue']!='N/A')
            {
                $row['ExpireValue']=number_format($row['ExpireValue']);
            }
            if($row['uviews']!='N/A')
            {
                $row['uviews']=number_format($row['uviews']);
            }
            if($row['clicks']!='N/A')
            {
                $row['priced'] = number_format($row['clicks']*$price);
                $row['clicks']=number_format($row['clicks']);
            }else{
                $row['priced'] = 'N/A';
            }
            if($row['impressions']!='N/A')
            {
                $row['impressions']=number_format($row['impressions']);
            }
			$data['lstCamp'][] = $row;
		}		
		mysqli_free_result($result);
		mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			$data['iCount'] = $row['num'];
		}
		$data['pCount'] = $data['iCount'];//($data['iCount'] > $numShowItem) ? ceil($data['iCount']/$numShowItem) : 1;
		//mysqli_free_result($result);	
//		mysqli_close($iconn);
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;    
	}
	
    function selfserving_campaign_update_inline($campaignid,$val,$type,$userid)
    {
        $data = array();
		//$iconn = mysqli_connect($this->_host,$this->_username,$this->_password, $this->_dbname,$this->_port);
//		$result = mysqli_query($iconn, "CALL selfserving_campaign_update_inline('$campaignid', '$val','$type','$userid');") or die(mysqli_error());
        $iconn = $this->db->conn_id;
		$result = mysqli_multi_query($iconn, "CALL selfserving_campaign_update_inline('$campaignid', '$val','$type','$userid');");
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		//mysqli_close($iconn);
		return $result;
    }
    
    function get_all_group()
    {
    	$data =array();
		//$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
//        $result = mysqli_query($iconn, "CALL selfserving_group_get_all();") or die(mysqli_error());
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_group_get_all();") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
        {
        	$data[] = $row;
        }
		//mysqli_free_result($result);
//		mysqli_close($iconn);
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data; 
    }
    
    function get_suggest_user($keyword)
    {
    	$data = array();
		//$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
//		$result = mysqli_query($iconn, "CALL selfserving_user_get_suggest('$keyword');") or die(mysqli_error($iconn));
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_user_get_suggest('$keyword');") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
		while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
		//mysqli_free_result($result);
//		mysqli_close($iconn);
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    
    function get_user_by_listid($strListId)
    {
    	$data = array();
		//$iconn = mysqli_connect($this->_host,$this->_username,$this->_password, $this->_dbname,$this->_port);
//		$result = mysqli_query($iconn, "CALL selfserving_admin_get_user_by_listid('$strListId');") or die(mysqli_error($iconn));
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_admin_get_user_by_listid('$strListId');") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
		while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
		//mysqli_free_result($result);
//		mysqli_close($iconn);
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    
	function get_allcampaign_chart($userid,$fromdate,$todate,$permitid,$lstcampaignid,$type)
    {
        $data = array();
        $data['campaigninfo']=array();
        $data['campaignname']=array();
		//$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_get_allcampaign_chart('$userid','$fromdate','$todate','$permitid','$lstcampaignid','$type');") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			$data['campaigninfo'][] = $row;
		}
        mysqli_free_result($result);
		mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['campaignname'][] = $row1;
		}
		//mysqli_free_result($result);
//		mysqli_close($iconn);
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    function selfservingGetListCampaign($userid, $states, $fromdate, $todate, $permitID)
	{
        $data=array();
        $data['lstCam']=array();
        $data['Cam']=array();
		//$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
        $iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL selfserving_get_listcampaigns('$userid', '$fromdate', '$todate', '$permitID' ,'$states');") or show_custom_error(mysqli_error($iconn));		
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			$data['lstCam'][] = $row;
		}
        mysqli_free_result($result);
		mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['Cam'][] = $row1;
		}
		//mysqli_free_result($result);
//		mysqli_close($iconn);
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
    
    //Export IPs
	function campaignExport_IPs($id, $fromdate, $todate)
    {
    	$data = array();
    	$data['items'] = array();
		//$iconn = mysqli_connect($this->_host,$this->_username,$this->_password, $this->_dbname,$this->_port);
        $iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL 100_rpt_campaign_ip($id,'$fromdate','$todate');") or die(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
		$row = mysqli_fetch_object($result);
        $data['name'] = $row->campaignname;
		mysqli_free_result($result);
        mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		while($row = mysqli_fetch_object($result))
		{
			$data['items'][] = $row;
		}
		//mysqli_free_result($result);
//		mysqli_close($iconn);
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    function selfserving_get_campaign_chart_hour($campaignid,$curdate,$type,$userid)
    {
        $data = array();
        $data['campaigninfo']=array();
        $data['campaignname']=array();
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_get_campaing_chart_hour('$campaignid','$curdate',$type,'$userid');") or die(mysqli_error());
		$result = mysqli_store_result($iconn);	
        while($row = mysqli_fetch_array($result))
		{
			$data['campaigninfo'][] = $row;
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['campaignname'][] = $row1;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    //end
}
?>